package com.example.mobile2021606185zhn0615;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class MyDataAdapter {
    private MySQLiteOpenHelper openHelper;
    private SQLiteDatabase database;
    public void openDB(Context context){
        openHelper = new MySQLiteOpenHelper(context,"student.db",null,1);
        try{
            database = openHelper.getReadableDatabase();
        }catch (Exception e){
            database = openHelper.getReadableDatabase();
        }
    }
         public void closeDB(){
        if (database!=null){
            database.close();
            database = null;
        }
        if (openHelper!=null){
            openHelper.close();
            openHelper = null;
        }
         }
         public List<Users> QuerAll(Context context){
        List<Users> usersList = new ArrayList<>();
        openDB(context);
             Cursor cursor = database.query("users",null,null,null,
                     null,null,null);
             if (cursor.moveToFirst()){
                 do {
                     int id = cursor.getInt(0);
                     String name = cursor.getString(1);
                     int age = cursor.getInt(2);
                     float score = cursor.getFloat(3);
                     usersList.add(new Users(id,name,age,score));
                 }while (cursor.moveToNext());
             }
             cursor.close();
             closeDB();
             return usersList;
         }

}
